package org.mm.mapper;

import java.util.Date;

import org.mm.model.Drug;
import org.mm.model.DrugType;
import org.mm.model.Measurement;
import org.mm.model.Patient;
import org.mm.model.Role;
import org.mm.model.Sensor;
import org.mm.model.SensorType;
import org.mm.model.Therapy;
import org.mm.model.TherapyTask;
import org.mm.model.TherapyTaskEvent;
import org.mm.model.TherapyType;
import org.mm.model.User;
import org.mm.xml.entities.DrugTypeXML;
import org.mm.xml.entities.DrugXML;
import org.mm.xml.entities.MeasurementXML;
import org.mm.xml.entities.PatientXML;
import org.mm.xml.entities.RoleXML;
import org.mm.xml.entities.SensorTypeXML;
import org.mm.xml.entities.SensorXML;
import org.mm.xml.entities.TherapyTaskEventXML;
import org.mm.xml.entities.TherapyTaskXML;
import org.mm.xml.entities.TherapyTypeXML;
import org.mm.xml.entities.TherapyXML;

public class Mapper {


	public static void mapUserToPatientXML(User user, PatientXML patientXML) {
		patientXML.setUserId(user.getUserId());
		patientXML.setUserName(user.getUserName());
		patientXML.setFirstName(user.getUserFirstName());
		patientXML.setLastName(user.getUserLastName());
		patientXML.setPhone(user.getUserPhone());
		patientXML.setEmail(user.getUserEmail());
		/*RoleXML roleXML = new RoleXML();
		mapRoleToRoleXML(user.getRole(), roleXML);
		patientXML.setRole(roleXML);*/
	}

	public static void mapRoleToRoleXML(Role role, RoleXML roleXML) {
		if (role != null) {
			roleXML.setId(role.getRoleId());
			roleXML.setName(role.getRoleName());
			roleXML.setDescription(role.getRoleDescription());
			roleXML.setAuthorithy(role.getRoleAuthority());
		}
	}

	public static void mapPatientToPatientXML(Patient patient, PatientXML patientXML) {
		patientXML.setId(patient.getPatientId());
		patientXML.setBirthDate(patient.getPatientBirthDate());
	}

	public static void mapTherapyToTherapyXML(Therapy therapy, TherapyXML therapyXML) {
		 mapTherapyToTherapyXML(therapy, therapyXML, true);
	}

	public static void mapTherapyToTherapyXML(Therapy therapy, TherapyXML therapyXML, boolean isLazy) {
		therapyXML.setId(therapy.getTherapyId());
		therapyXML.setName(therapy.getTherapyShortDescription());
		therapyXML.setDescription(therapy.getTherapyDescription());
		TherapyTypeXML therapyTypeXML = new TherapyTypeXML();
		mapTherapyTypeToTherapyTypeXML(therapy.getTherapyType(), therapyTypeXML);
		therapyXML.setTherapyType(therapyTypeXML);
		if (therapy.getTherapyTasks() != null) {
			for (TherapyTask therapyTask : therapy.getTherapyTasks()) {
				TherapyTaskXML therapyTaskXML = new TherapyTaskXML();
				mapTherapyTaskToTherapyTaskXML(therapyTask, therapyTaskXML, isLazy);
				therapyXML.addTherapyTask(therapyTaskXML);
			}
		}
	}

	public static void mapTherapyTypeToTherapyTypeXML(TherapyType therapyType, TherapyTypeXML therapyTypeXML) {
		therapyTypeXML.setId(therapyType.getTherapyTypeId());
		therapyTypeXML.setName(therapyType.getTherapyTypeShortDescription());
		therapyTypeXML.setDescription(therapyType.getTherapyTypeDescription());
	}

	public static void mapTherapyTaskToTherapyTaskXML(TherapyTask therapyTask, TherapyTaskXML therapyTaskXML) {
		mapTherapyTaskToTherapyTaskXML(therapyTask, therapyTaskXML, true);
	}

	public static void mapTherapyTaskToTherapyTaskXML(TherapyTask therapyTask, TherapyTaskXML therapyTaskXML, boolean isLazy) {
		therapyTaskXML.setId(therapyTask.getTherapyTaskId());
		therapyTaskXML.setName(therapyTask.getTherapyTaskShortDescription());
		therapyTaskXML.setDescription(therapyTask.getTherapyTaskDescription());
		therapyTaskXML.setStartDate(therapyTask.getTherapyTaskStartDate());
		therapyTaskXML.setEndDate(therapyTask.getTherapyTaskEndDate());
		therapyTaskXML.setCronExpression(therapyTask.getTherapyTaskCronExpression());
		therapyTaskXML.setDrug(mapDrugToDrugXML(therapyTask.getDrug()));
		therapyTaskXML.setSensor(mapSensorToSensorXML(therapyTask.getSensor()));
		if (!isLazy) {
			if (therapyTask.getTherapyTaskEvents() != null) {
				for (TherapyTaskEvent therapyTaskEvent : therapyTask.getTherapyTaskEvents()) {
					therapyTaskXML.addTherapyTaskEvent(mapTherapyTaskEventToTherapyTaskEventXML(therapyTaskEvent));
				}
			}
		}
	}

	public static DrugXML mapDrugToDrugXML(Drug drug) {
		DrugXML drugXML = null;
		if (drug != null) {
			drugXML = new DrugXML();
			mapDrugToDrugXML(drug, drugXML);
		}
		return drugXML;
	}

	public static void mapDrugToDrugXML(Drug drug, DrugXML drugXML) {
		if (drug != null && drugXML != null) {
			drugXML.setId(drug.getDrugId());
			drugXML.setQuantity(drug.getDrugQuantity());
			DrugTypeXML drugTypeXML = new DrugTypeXML();
			mapDrugTypeToDrugTypeXML(drug.getDrugType(), drugTypeXML);
			drugXML.setDrugType(drugTypeXML);
		}
	}

	public static void mapDrugTypeToDrugTypeXML(DrugType drugType, DrugTypeXML drugTypeXML) {
		if (drugType != null && drugTypeXML != null) {
			drugTypeXML.setId(drugType.getDrugTypeId());
			drugTypeXML.setName(drugType.getDrugTypeShortDescription());
			drugTypeXML.setDescription(drugType.getDrugTypeDescription());
		}
	}

	public static SensorXML mapSensorToSensorXML(Sensor sensor) {
		SensorXML sensorXML = null;
		if (sensor != null) {
			sensorXML = new SensorXML();
			mapSensorToSensorXML(sensor, sensorXML);
		}
		return sensorXML;
	}

	public static void mapSensorToSensorXML(Sensor sensor, SensorXML sensorXML)  {
		if (sensor != null && sensorXML != null) {
			sensorXML.setId(sensor.getSensorId());
			sensorXML.setIdentifier(sensor.getSensorIdentifier());
			SensorTypeXML sensorTypeXML = new SensorTypeXML();
			mapSensorTypeToSensorTypeXML(sensor.getSensorType(), sensorTypeXML);
			sensorXML.setSensorType(sensorTypeXML);
			sensorXML.setFakeMeasureValue(sensor.getSensorFakeMeasureValue());
		}

	}

	public static void mapSensorTypeToSensorTypeXML(SensorType sensorType, SensorTypeXML sensorTypeXML) {
		if (sensorType != null && sensorTypeXML != null) {
			sensorTypeXML.setId(sensorType.getSensorTypeId());
			sensorTypeXML.setName(sensorType.getSensorTypeShortDescription());
			sensorTypeXML.setDescription(sensorType.getSensorTypeDescription());
		}
	}

	public static TherapyTaskEventXML mapTherapyTaskEventToTherapyTaskEventXML(TherapyTaskEvent therapyTaskEvent) {
		TherapyTaskEventXML therapyTaskEventXML = null;
		if (therapyTaskEvent != null) {
			therapyTaskEventXML = new TherapyTaskEventXML();
			mapTherapyTaskEventToTherapyTaskEventXML(therapyTaskEvent, therapyTaskEventXML);

		}
		return therapyTaskEventXML;
	}


	public static void mapTherapyTaskEventToTherapyTaskEventXML(TherapyTaskEvent tte, TherapyTaskEventXML ttex) {
		if (tte != null && ttex != null) {
			ttex.setScheduledDate(new Date(tte.getTherapyTaskEventScheduledDate().getTime()));
			ttex.setExecutionDate(new Date(tte.getTherapyTaskEventExecutionDate().getTime()));
			ttex.setMeasurement(mapMeasurementToMeasurementXML(tte.getMeasurement()));
		}
	}

	public static MeasurementXML mapMeasurementToMeasurementXML(Measurement m) {
		MeasurementXML mx = null;
		if (m != null) {
			mx = new MeasurementXML();
			mapMeasurementToMeasurementXML(m, mx);
		}
		return mx;
	}

	public static void mapMeasurementToMeasurementXML(Measurement m, MeasurementXML mx) {
		if (m != null && mx != null) {
			mx.setId(m.getMeasurementId());
			mx.setDescription(m.getMeasurementDescription());
			mx.setData(m.getMeasurementData());
			mx.setCreationDate(m.getMeasurementCreationDate());
			if (m.getPatient() != null) {
				mx.setPatientId(m.getPatient().getPatientId());
			}
			if (m.getSensor() != null) {
				mx.setSensorId(m.getSensor().getSensorId());
			}
		}
	}

}
